<?php
/**
 * Created by helloJiu.
 * Idea: 认真编码 快乐生活
 * Date: 2020/12/27
 * Time: 17:31
 */

namespace App\UnionFind;

use App\Util\RandUtil;
use App\Util\TimeUtil;

require "../../vendor/autoload.php";

class UnionTest
{
    public static function testUnion()
    {
        $data = [1,2,4,5,6,8,9, 10, 11,3];
        //$uf = new UnionFindQF($data);
        //$uf = new UnionFindQU($data);
        //$uf = new UnionFindQUSize($data);
        $uf = new UnionFindQUHeight($data);
        $uf->union(1, 2);
        $uf->union(2, 4);
        $uf->union(6, 2);
        $uf->union(8, 9);
        $uf->union(10, 11);
        $uf->union(1, 8);
        //$uf->union(1,10);
        $uf->union(3, 1);

        var_dump($uf->isSame(1, 6));
        var_dump($uf->isSame(8, 10));
        var_dump($uf->isSame(1, 9));
        print_r($uf->getData());
        print_r($uf->getHeight());
    }

    public function randUfSizeTest(AbsUnionFind $uf, $data)
    {
        $length = count($data);
        for ($i = 0; $i < count($data); $i++) {
            $uf->union($data[random_int(1, $length-1)], $data[random_int(1, $length-1)]);
            $uf->isSame($data[random_int(1, $length-1)], $data[random_int(1, $length-1)]);
        }
    }
}

UnionTest::testUnion();
//
// ini_set('memory_limit','1200M');
$data = RandUtil::getIntegers(100000, 1000000);
$ufs = [
    //new UnionFindQF($data),
    //new UnionFindQU($data),
    new UnionFindQUSize($data),
    new UnionFindQUHeight($data)
];

foreach ($ufs as $uf){
    $test = new UnionTest();
    echo get_class($uf) . PHP_EOL;
    TimeUtil::testFunc($test,'randUfSizeTest', $uf, $data);
}

/*
 * $length=10000
App\UnionFind\UnionFindQF
-----------App\UnionFind\UnionTest->randUfSizeTest spent time: 6.83(s)----6833.39(ms)----------
App\UnionFind\UnionFindQU
-----------App\UnionFind\UnionTest->randUfSizeTest spent time: 0.13(s)----131.28(ms)----------
App\UnionFind\UnionFindQUSize
-----------App\UnionFind\UnionTest->randUfSizeTest spent time: 0.01(s)----10.67(ms)----------
App\UnionFind\UnionFindQUHeight
-----------App\UnionFind\UnionTest->randUfSizeTest spent time: 0.01(s)----9.83(ms)----------

$length = 2000000;
App\UnionFind\UnionFindQUSize
-----------App\UnionFind\UnionTest->randUfSizeTest spent time: 2.83(s)----2826.25(ms)----------
App\UnionFind\UnionFindQUHeight
-----------App\UnionFind\UnionTest->randUfSizeTest spent time: 2.92(s)----2918.34(ms)----------
 */

